/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import javacommon.util.ResInfo;
import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.Dept;
import com.tech15.core.model.SysUser;
import com.tech15.pub.model.PubGroup;
import com.tech15.pub.model.PubGroupUser;
import com.tech15.pub.service.PubGroupManager;
import com.tech15.pub.vo.query.PubGroupQuery;
import com.tech15.pub.vo.query.PubGroupUserQuery;


/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubGroupAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String LIST_JSP= "/pub/pubGroup_list.jsp";
	protected static final String GROUPUSERS_JSP= "/pub/pubGroup_user_list.jsp";
	protected static final String PRIGROUP_JSP= "/pub/pubGroup_select.jsp";
	protected static final String CREATE_JSP = "/pub/pubGroup_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubGroup_edit.jsp";
	protected static final String SHOW_JSP = "/pub/pubGroup_show.jsp";
	//redirect paths,startWith: !
	
	private PubGroupManager pubGroupManager;
	private PubGroup pubGroup;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubGroup = new PubGroup();
		} else {
			pubGroup = (PubGroup)pubGroupManager.getById(id);
		}
	}
	
	public void setPubGroupManager(PubGroupManager manager) {
		this.pubGroupManager = manager;
	}
	
	public Object getModel() {
		return pubGroup;
	}
	
	public void setPgid(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	/** 执行搜索 */
	public String list() {
		PubGroupQuery query = newQuery(PubGroupQuery.class,DEFAULT_SORT_COLUMNS);
		query.setUserid(getLoginInfo().getUserid());
		Page page = pubGroupManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	public String usersInPubGroup() {
		PubGroupUserQuery query = newQuery(PubGroupUserQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = pubGroupManager.searchPubGroupUsers(query);
		savePage(page,query);
		return GROUPUSERS_JSP;
	}
	
	public String selectPubGroup(){
		List<PubGroup> list = pubGroupManager.selectPubGroup();
		getRequest().setAttribute("pglist", list);
		return PRIGROUP_JSP;
	}
	
	public void selectGroup() {
		Map map = new HashMap();
		List<PubGroup> list = pubGroupManager.selectGroup(getLoginInfo().getUserid());
		PubGroup pg = new PubGroup();
		pg.setName("请选择");
		list.add(0, pg);
		map.put("totalRecords", -1);
		map.put("root", list);
		outJson(map);
	}
	
	public void searchUser(){
		Map map = new HashMap();
		String realname = "";
		try {
			if(!isNullOrEmptyString(key)){
				realname = new String(key.getBytes("ISO-8859-1"),"UTF-8");
			}
			realname = key;
			List<SysUser> list = pubGroupManager.selectUser(keyid,realname);
			map.put("totalRecords", 0);
			map.put("root", list);
			outJson(map);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
		Map<String, Object> resInfo = null;
		try{
			pubGroup.setuserid(getLoginInfo().getUserid());
			pubGroupManager.save(pubGroup);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		List<PubGroupUser> list = pubGroupManager.selectPubGroupUsersByPgid(id);
		getRequest().setAttribute("pglist",list);
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			pubGroup.setuserid(getLoginInfo().getUserid());
			pubGroupManager.update(this.pubGroup);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try{
			pubGroupManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}

	private String key;
	private Long keyid;

	public Long getKeyid() {
		return keyid;
	}

	public void setKeyid(Long keyid) {
		this.keyid = keyid;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	

}
